﻿using System;
using PMS.XCMG.Entity;
using PMS.XCMG.IBusiness;
using PMS.XCMG.Models;
using PMS.XCMG.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace PMS.XCMG.Api.Controllers
{
    /// <summary>
    /// Precollected
    /// </summary>
    public class PrecollectedController : BaseController
    {
        /// <summary>
        /// 
        /// </summary>
        public IPrecollectedBusiness Bus { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IPlanBusiness PBus { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IMaterialBomTreeBusiness BtBus { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IServiceProvider SvcProvider { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bus"></param>
        /// <param name="btbus"></param>
        /// <param name="pbus"></param>
        /// <param name="svcProvider"></param>
        public PrecollectedController(IPrecollectedBusiness bus, IMaterialBomTreeBusiness btbus, IPlanBusiness pbus, IServiceProvider svcProvider)
        {
            this.Bus = bus;
            this.PBus = pbus;
            this.BtBus = btbus;
            this.SvcProvider = svcProvider;
        }

        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public Task<Precollected> GetAsync(string id)
        {
            return this.Bus.GetAsync(id, true);
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public Task<PageResult<Precollected>> GetPageAsync(PageInput<PrecollectedQM> query)
        {
            return this.Bus.GetPageResultAsync(query);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="data">对应数据</param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public Task<int> SaveAsync(Precollected data)
        {
            this.InitBusEntity(data);
            return this.Bus.AddOrUpdateAsync(data);
        }

        /// <summary>
        /// 生成预收清单
        /// </summary>
        /// <param name="planId">计划编号</param>
        /// <param name="bomId">BOM编号</param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public Task<int> BuildAsync(string planId,string bomId)
        {
            List<Precollected> list = new List<Precollected>();
            var plan = PBus.GetAsync(planId).Result;
            var bomTrees = BtBus.GetTreeDataAsync(bomId).Result;

            var mitems = (from u in bomTrees where u.Materiel.Type.Code == "YCL" select u).ToList();
            foreach(var item in mitems)
            {
                Precollected entity = new Precollected();
                entity.PlanId = planId;
                entity.BomTreeId = item.Id;
                entity.MaterielId = item.MaterialId;
                entity.MaterieCode = item.Materiel.Code;
                entity.MaterieName = item.Materiel.Name;
                entity.SpecId = item.SpecId;
                entity.SpecCode = item.Spec.Code;
                entity.SpecName = item.Spec.Name;
                entity.Quantity = item.Quantity * plan.Count;
                entity.RealIncome = 0;
                

                this.InitBusEntity(entity);
                list.Add(entity);
            }

            return this.Bus.AddAsync(list);
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="ids">数据主键</param>
        /// <returns></returns>
        [HttpDelete]
        [Transaction]
        public Task<int> DeleteAsync(List<string> ids)
        {
            return this.Bus.DeleteAsync(ids);
        }
    }
}
